package com.batdream.message.netty;

import com.batdream.message.utils.TimeUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author Wang Chen Peng
 * @date 2018/12/24
 * describe:
 */
@Component
public class MessageServer {
    private static final NioEventLoopGroup BOSS_GROUP;
    private static final NioEventLoopGroup WORKER_GROUP;
    private static final ServerBootstrap BOOT_STRAP;
    private static ChannelFuture channelFuture;

    private static final MessageServer INSTANCE;

    static {
        INSTANCE = new MessageServer();
        if (Objects.isNull(INSTANCE)){
            System.out.println("null");
        }
        BOSS_GROUP = new NioEventLoopGroup();
        WORKER_GROUP = new NioEventLoopGroup();
        BOOT_STRAP = new ServerBootstrap();
        BOOT_STRAP.group(BOSS_GROUP, WORKER_GROUP)
                .channel(NioServerSocketChannel.class)
                .childHandler(new WorkerServiceHandle());
    }

    private MessageServer(){

    }

    /*public MessageServer() {
        bossGroup = new NioEventLoopGroup();
        wockerGroup = new NioEventLoopGroup();
        bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, wockerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(null);
    }*/

    /*private static class SingletonMessageServer {
        static final MessageServer INSTANCE = new MessageServer();

    }*/

    public static MessageServer getInstance() {

        return INSTANCE;

    }

    public void start() {

        channelFuture = BOOT_STRAP.bind(8081);
        System.out.println(TimeUtil.getTime()+"  MessageServer on 8081 port 启动完毕...");
        channelFuture.channel().closeFuture();

    }


}
